﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MovieLister
{
    //A very good article of Dependency Control
    //http://ayende.com/blog/2887/dependency-injection-doesnt-cut-it-anymore
    //http://ayende.com/blog/2886/building-an-ioc-container-in-15-lines-of-code
    class Program
    {
        static void Main(string[] args)
        { 
            MovieLister ml = new MovieLister();
            Movie[] movieList = ml.moviesDirectedBy("Steven Spielberg");
            for (int i = 0; i <= movieList.Count() -1; i++)
              {
                  Console.WriteLine(movieList[i].DirectorName );
              }
             Console.ReadLine(); 
        }
    }

    public class Movie
    {
        private string _directorName;

        public string DirectorName
        {
            get { return _directorName; }
            set { _directorName = value; }
        }

        public Movie (string directoryName)
        {
            this.DirectorName = directoryName;
        }
    }

    public interface IMovieFinder
    {
        List<Movie> findAll();
    }

    public class MovieFinder : IMovieFinder
    {
        public List<Movie> findAll()
        {
            List<Movie> result = new List<Movie>();
            result.Add(new Movie("A"));
            result.Add(new Movie("B"));
            return result ;
        }
    }

    class MovieLister
    {
        private IMovieFinder finder;
        public MovieLister(IMovieFinder finder)
        {
            this.finder = finder; 
        }
        public Movie[] moviesDirectedBy(String arg)
        {
           //IMovieFinder finder = new MovieFinder();
            List<Movie> allMovies = finder.findAll();
            //for (Iterator it = allMovies.iterator(); it.hasNext(); )
            //{
            //    Movie movie = (Movie)it.next();
            //    if (!movie.getDirector().equals(arg)) it.remove();
            //}
            return (Movie[])allMovies.ToArray();
        }
    }

    //public class DemoContainer
    //{
    //    public delegate object Creator(DemoContainer container);
    //    private readonly Dictionary<string, object> configuration = new Dictionary<string, object>();
    //    private readonly Dictionary<Type, Creator> typeToCreator = new Dictionary<Type, Creator>();

    //    public Dictionary<string, object> Configuration
    //    {
    //        get { return configuration; }
    //    }

    //    public void Register<T>(Creator creator)
    //    {
    //        typeToCreator.Add(typeof(T), creator);
    //    }

    //    public T Create<T>()
    //    {
    //        return (T)typeToCreator[typeof(T)](this);
    //    }

    //    public T GetConfiguration<T>(string name)
    //    {
    //        return (T)configuration[name];
    //    } 
    //}
} 